﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Data;
using System.Data;
using System.Data.SqlClient;

namespace Bll
{
    public class [tblName]Bll : baseBLL
    {

        #region 增删改
        public bool Add([tblName] entity)
        {
            var db = new Data.DataEntities();
            db.[tblName].Add(entity);
            return db.SaveChanges() > 0;
        }
        public bool Update([tblName] entity)
        {
            if (entity.id <= 0) return Add(entity);
            var db = new Data.DataEntities();
            var mod = db.[tblName].FirstOrDefault(p => p.id == entity.id);
            mod = Mapper(entity, mod);
            return db.SaveChanges() > 0;
        }
        public bool DeleteById(int id)
        {
            var db = new Data.DataEntities();
            var mod = db.[tblName].FirstOrDefault(p => p.id == id);
            db.[tblName].Remove(mod);
            int rows = db.SaveChanges();
            return rows > 0;
        }
        public bool DeleteByIds(string ids)
        {
            string[] idsArr = ids.Split(',');
            for (int i = 0; i < idsArr.Length; i++)
            {
                int id = 0;
                int.TryParse(idsArr[i], out id);
                if (id > 0)
                {
                    DeleteById(id);
                }
            }
            return true;
        }
        #endregion

        #region 查询
        public [tblName] GetById(int id)
        {
            var db = new Data.DataEntities();
            return db.[tblName].FirstOrDefault(p => p.id == id);
        }

        /// <summary>
        /// 把dic形式的输入参数，转换成strwhere和paralist
        /// 如果多个报表，可复制多个函数
        /// </summary>
        private string dicToParaList(Dictionary<string, string> dicCondition, out List<SqlParameter> paraList)
        {
            paraList = new List<SqlParameter>();
            var sb = new StringBuilder(256);

            #region 查询条件
			[loop start]
            if (dicCondition.ContainsKey("[fldName]")) //按[fldTitle] 进行查询
            {
                string [fldName] = dicCondition["[fldName]"];
                if (!string.IsNullOrWhiteSpace([fldName]))
                {
                    sb.Append(" and m.[fldName]=@[fldName]");
                    paraList.Add(new SqlParameter("@[fldName]", [fldName]));
                }
            }
             [loop end]

            #endregion

            #region 排序
            string orderField = "m.id desc";
            if (dicCondition.ContainsKey("orderby"))
            {
                orderField = dicCondition["orderby"];
            }
            #endregion
            sb.Append(" order by " + orderField);
            return sb.ToString();
        }
        public DataSet GetDataset(Dictionary<string, string> dicCondition)
        {
            string fields = "*";
            //fields =  "id,m.Cataname,Filename,About,Adddate,Person,Ext1,Ext2,Ext3,Attachcount";
            string sql = "select " + fields + " from [tblName] as m where 1=1 ";

            List<SqlParameter> paraList = new List<SqlParameter>();
            string strWhere = dicToParaList(dicCondition, out paraList);
            sql += strWhere;

            return Execute(sql, paraList); ;
        }
        public DataTable GetPageDataTable(Dictionary<string, string> dicCondition, int currentPage, int pageSize, out int totalRecord)
        {
            var dset = GetDataset(dicCondition);
            totalRecord = dset.Tables[0].Rows.Count;
            var dt = LIB.pager.getCurrentPageDataTable(dset, currentPage, pageSize);
            return dt;
        }

        #endregion
    }
}



